const util = {
  removeEmptyField(obj) { // 过滤掉空字段
    var newObj = {}
    if (typeof obj === 'string') {
      obj = JSON.parse(obj)
    }
    if (obj instanceof Array) {
      newObj = []
    }
    if (obj instanceof Object) {
      for (var attr in obj) {
        // 属性值不为'',null,undefined才加入新对象里面(去掉'',null,undefined)
        if (obj.hasOwnProperty(attr) && obj[attr] !== '' && obj[attr] !== null && obj[attr] !== undefined) {
          if (obj[attr] instanceof Object) {
            // 空数组或空对象不加入新对象(去掉[],{})
            if (JSON.stringify(obj[attr]) === '{}' || JSON.stringify(obj[attr]) === '[]') {
              continue
            }
            // 属性值为对象,则递归执行去除方法
            newObj[attr] = util.removeEmptyField(obj[attr])
          } else if (
            typeof obj[attr] === 'string' &&
            ((obj[attr].indexOf('{') > -1 && obj[attr].indexOf('}') > -1) ||
              (obj[attr].indexOf('[') > -1 && obj[attr].indexOf(']') > -1))
          ) {
            // 属性值为JSON时
            try {
              var attrObj = JSON.parse(obj[attr])
              if (attrObj instanceof Object) {
                newObj[attr] = util.removeEmptyField(attrObj)
              }
            } catch (e) {
              newObj[attr] = obj[attr]
            }
          } else {
            newObj[attr] = obj[attr]
          }
        }
      }
    }
    return newObj
  },
  selectArr(a, arr, name) { // 加name 对比值
    let Arr = arr
    let size = Arr.length
    Arr.forEach((item, index) => {
      if (name) {
        if (item[name] == a[name]) {
          Arr.splice(index, 1)
        }
      } else {
        if (item == a) {
          Arr.splice(index, 1)
        }
      }
    })
    if (size == Arr.length) {
      Arr.push(a)
    }
    return Arr
  },
  getMaximin(arr, maximin) { // 获取数组中最大最小值
    if (maximin == "max") {
      return Math.max.apply(Math, arr)
    } else if (maximin == "min") {
      return Math.min.apply(Math, arr)
    }
  },
  clearInteger (mun) { // 只能输入整数
    if (!mun) return
    let data = mun
    data = data.replace(/\-/g, "")
    data = data.replace(/[^[1-9]\d]/g, "")
    data = data.replace(/\b(0+)/gi,"")
    data = Number(data)
    return data
  },
  clearNoNum(mun, type) { // 保留2位小数
    if (!mun) return
    let data = mun
    data = data.replace(/\-/g, "")
    data = data.replace(/[^\d.]/g, "")
    data = data.replace(/\.{2,}/g, ".")
    if (data.length == 1 && data == '.') {
      data = 0 + data
    }
    if (data.split('.')[0].length > 1) {
      data = data.replace(/\b(0+)/gi, "")
    }
    data = data.replace(/^\./g, "")
    data = data.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".")
    let arr = data.split('.')
    if (arr && arr.length > 0 && !type) {
      if (arr[1] && (arr[1].length > 2)) {
        data = Number(data).toFixed(2)
      }
    }
    return data
  },
  returnFloat(a) { // 自动补全2位小数
    if (Number(a) === 0) {
      return 0.00
    }
    if (!a) return
    let mun = a
    mun = Math.round(parseFloat(mun) * 100) / 100
    let xsd = mun.toString().split(".")
    if (xsd.length == 1) {
      mun = mun.toString() + ".00"
      return mun
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        mun = mun.toString() + "0"
      }
      return mun
    }
  },
  unique(arr) {
    var arr1 = []
    for (var i = 0, len = arr.length; i < len; i++) {
      if (!arr1.includes(arr[i])) {      // 检索arr1中是否含有arr中的值
        arr1.push(arr[i])
      }
    }
    return arr1
  },
  mobileStr(str) {
    var tel = str
    tel = "" + tel
    var tel1 = tel.substr(0,3) + "****" + tel.substr(7)
    return tel1
  },
  countdownPay(endTime,noDay) {
    let end = endTime // 这个是时间戳
    let now = Date.parse(new Date())
    let msec = end - now
    if(msec<0) return
    let day = parseInt(msec / 1000 / 60 / 60 / 24)
    let hr = parseInt(msec / 1000 / 60 / 60 % 24)
    let min = parseInt(msec / 1000 / 60 % 60)
    let sec = parseInt(msec / 1000 % 60)
    day = day
    hr = hr > 9 ? hr : '0' + hr
    min = min > 9 ? min : '0' + min
    sec = sec > 9 ? sec : '0' + sec
    if (Number(min)>=0 && Number(sec)>=0) {
      if(Number(day)<=0 && Number(hr)<=0 && Number(min)<=0 && Number(sec)<=0){
        return {
          code: 201,
          day: '00',
          hr: '00',
          min: '00',
          sec: '00'
        }
      }
    }
    return {
      code: 200,
      day: day,
      hr: noDay ? (day * 24) + Number(hr) : hr,
      min: min,
      sec: sec
    }
  },
  showTime (str, type) {
    let str_ = new Date(str.replace(/-/g, "/"))
    let data = {
      day: 0,
      hour: 0,
      minute: 0,
      second: 0
    }
    if (!str_) return data
    const time = [1,2].includes(type) ? +new Date(str_) : +new Date(str_)
    // 获取当前时间
    const nowTime = +new Date()
    // 获取时间差
    // if (time < nowTime) data
    const timediff = [1,2].includes(type) ? Math.round((nowTime - time) / 1000) : Math.round((time - nowTime) / 1000)
    // 获取还剩多少天
    const day = parseInt(timediff / 3600 / 24)
    // 获取还剩多少小时
    const hour = parseInt(timediff / 3600 % 24)
    // 获取还剩多少分钟
    const minute = parseInt(timediff / 60 % 60)
    // 获取还剩多少秒
    const second = timediff % 60
    // 输出还剩多少时间
    // console.log(day+"天"+timerFilter(hour)+"时"+timerFilter(minute)+"分"+timerFilter(second)+"秒")
    // 给小于10的数值前面加 0
    function timerFilter (params) {
      if (params>0&&params<10) {
        return '0' + params
      } else {
        return params
      }
    }
    if (type == 1) {
      data = {
        day: day,
        hour: hour,
        minute: minute,
        second: second
      }
    } else if (type == 2) {
      if (Number(day)==0&&(Number(hour)>0||Number(minute)>0||Number(second)>0)) {
        data = {
          day: 1,
          hour: 0,
          minute: 0,
          second: 0
        }
      } else {
        data = {
          day: day,
          hour: hour,
          minute: minute,
          second: second
        }
      }
    } else {
      data = {
        day: day,
        hour: timerFilter(hour),
        minute: timerFilter(minute),
        second: timerFilter(second)
      }
    }
    if (type == 1) {
      if (time == nowTime) return '刚刚'
      if (data.day > 0) {
        const da = new Date(str_)
        const year = da.getFullYear() + '年'
        const month = da.getMonth() + 1 + '月'
        const date = da.getDate() + '日'
        if (data.day >= 365) {
          return year + month + date
        } else if (data.day > 30) {
          return month + date
        } else if (data.day == 30 ) {
          return '一个月前'
        } if (data.day == 7 ) {
          return '一周前'
        } else {
          return data.day + '天前'
        }
      }
      if (data.hour > 0 && data.day < 1) {
        return data.hour + '小时前'
      }
      if (data.minute > 0 && data.hour < 1) {
        return data.minute + '分钟前'
      }
      if (data.minute < 1 && data.second > 0) {
        return '1分钟前'
      }
    } else if(type == 2) {
      return data
    } else {
      return data
    }
  },

  /*
  * @description 取截单元,单位
  * @param arg 原始数据
  * @param unit 显示单位--默认w
  * @param exceed  是否超100000显示10w+--默认为true
  * @param isMoney  是否是金额数字 --> 先保留--防止后续需求变更 , isMoney = false
  * */
  playvolume (arg, unit = '万', exceed = false, noFlot) {
    if (!arg) return 0
    // let flag = arg.toString().length > 4
    // if (isMoney) {
    //   // 金额--判断整数部分
    //   const intNum = parseInt(arg)
    //   flag = intNum.toString().length > 4
    // }

    // 金额--判断整数部分
    const intNum = parseInt(arg)
    const flag = intNum.toString().length > 4

    if (flag) {
      if (exceed && (arg >= 100000)) return `10${unit}+`

      const volume = arg / 10000
      let realVal = Math.floor(volume * 100) / 100
      if(noFlot){
        return  realVal + unit
      }
      if (realVal.toString().indexOf('.') < 0) realVal += '.00'

      return realVal + unit
    } else {
      return arg
    }
    // if(arg.toString().length>4&&arg<100000){
    //   const volume = arg/10000
    //   const realVal = parseFloat(volume).toFixed(2)
    //   return realVal + unit
    // } else if (arg>=100000) {
    //   return "10w+"
    // } else {
    //   return arg
    // }
  },
  monthComparison (t1,t2) {
    let tiemArr1 = t1.split('-')
    let tiemArr2 = t2.split('-')
    let data = ''
    if (tiemArr1[0]!=tiemArr2[0] || tiemArr1[1]!=tiemArr2[1]) {
      data = tiemArr2[0] + '年' + tiemArr2[1] + '月'
    }
    return data
  },
  bankAccountValid(value){
    const strBin = '10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99'
    if (!value) {
      return '银行卡号不能为空'
    } else if (!Number.isInteger(+value)) {
      return '银行卡号必须全为数字'
    } else if (value.trim().length < 12 || value.trim().length > 19) {
      return '银行卡号长度必须在12到19之间'
    } else if (strBin.indexOf(value.substring(0, 2)) === -1) {
      return '银行卡号开头6位不符合规范'
    } else {
      return 200
    }
  },
  bankStr (value,type) {
    let reg = /^(\d{4})\d+(\d{4})$/
    if (type==1) {
      return value.replace(reg,"$1 **** **** $2")
    } else {
      return value.replace(reg,"**** **** **** $2")
    }
  },
  coverImgHandle (url,w,h) {
    if (!url) return
    let url_ = url
    let mun = url_.indexOf('?x-oss-process=video')>0
    if (mun>0) {
      url_ = url_+`,w_${w},h_${h}`
    } else {
      url_ = url_+`?x-oss-process=image/resize,w_${w},h_${h},m_fill/auto-orient,1/format,jpg`
    }
    return url_
  },
  contrastTime (data,str) { // 截取掉时间的时分秒
    let sj = data.split(" ")[0]
    if (str) {
      sj = sj.replace(/-/g,str)
    }
    return sj
  },
}
export default util
